package com.mrjy.coursepackage.bo;

import com.mrjy.common.entity.CoursePackageInfo;
import com.mrjy.common.entity.UserCoursePackageRel;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResponseCode;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dao.ICoursepackageVideoTypeRelDao;
import com.mrjy.coursepackage.dao.IUserCoursePackageDao;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.maintain.dao.IUserInfoDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserCoursePackageBo implements IUserCoursePackageBo {
	
	@Resource
	private IUserCoursePackageDao userCoursePackageDao;
	@Resource
	private ICoursepackageVideoTypeRelDao coursepackageVideoTypeRelDao;
	@Resource
	private ICoursePackageInfoDao coursePackageInfoDao;
    @Resource
    private IUserInfoDao userInfoDao;

	@Override
	public Object queryByUserIdAndCoursePackageId(UserCoursePackageRel rel) {
		Map<String, Object> map  = new HashMap<String, Object>();
		if (null==rel.getUserId() || null==rel.getCoursePackageId()) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		CoursePackageInfoDTO vo = new CoursePackageInfoDTO();
		vo.setId(rel.getCoursePackageId());
		CoursePackageInfo coursePackageInfo = coursePackageInfoDao.selectById(vo);
		if (coursePackageInfo==null || coursePackageInfo.getStageTabId() == null) {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败,未查询到课程包或该课程包未与阶段标签绑定");
			return map;
		}
		UserCoursePackageRel coursePackage = userCoursePackageDao.queryByUserIdAndCoursePackageId(rel);
		int row = 0;
		//更新访问时间
		rel.setAccessTime(new Timestamp(System.currentTimeMillis()));
		//synchronized (this) {
			if (coursePackage!=null) { //已有浏览该课程包记录,则更新click_count
				rel.setClickCount(coursePackage.getClickCount()+1);
				row = userCoursePackageDao.updateByUserIdAndCoursePackageId(rel);
			}else {  //没有记录,增新增
				if(rel.getUserId().equals(0)){
					map.put("err_code", ResponseCode.VISITOR_STATUS.getCode());
					map.put("err_msg", ResponseCode.VISITOR_STATUS.getDesc());
					return map;
				}
				rel.setTypeId(coursePackageInfo.getStageTabId());
				rel.setClickCount(1); //第一次浏览,默认1
				try {
					row = userCoursePackageDao.insert(rel);
				} catch (Exception e) {
					//数据库user_coursepackage_rel表中建立了唯一索引
					//捕获插入重复数据报的错,执行update
					e.printStackTrace();
					rel.setClickCount(coursePackage.getClickCount()+1);
					row = userCoursePackageDao.updateByUserIdAndCoursePackageId(rel);
				}
			}
			//增加课程包在学人数
			coursePackageInfo.setClickCount(coursePackageInfo.getClickCount()+1);
			coursePackageInfoDao.updateById(coursePackageInfo);
		//}
		if (row >0) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
		}
		return map;
	}

	@Override
	public Object hasClickHistory(Integer userId) {
		Map<String,Object> map = new HashMap<String,Object>();
		if (userId==null) {
			map.put("err_code",400);
			map.put("err_msg", "参数异常");
			return map;
		}
		int count = userCoursePackageDao.hasClickHistory(userId);
		if (count>0) { //有点击过
			map.put("err_code",0);
			map.put("err_msg", "点击过课程包");
		}else {
			map.put("err_code",1);
			map.put("err_msg", "未点击过");
		}
		return map;
	}

    @Override
    public Object queryClassmate(UserCoursePackageRel userCoursePackageRel, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (userCoursePackageRel.getCustomModuleId() == null) {
            map.put("err_code", 400);
            map.put("err_msg", "参数异常");
            return map;
        }
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == loginUser) {
            map.put("err_code", -1);
            map.put("err_msg", "用户未登录");
            return map;
        }
        List<UserCoursePackageRel> list = userCoursePackageDao.queryClassmate(userCoursePackageRel);

        int total = userCoursePackageDao.countClassmate(userCoursePackageRel);
        //自我的成绩
        userCoursePackageRel.setUserId(loginUser.getId());
        List<UserCoursePackageRel> list2 = userCoursePackageDao.queryClassmate(userCoursePackageRel);
        if (list.size() > 0 && list2.size() > 0) { //有点击过
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
            map.put("total", total);
            map.put("info", list2.get(0));
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "查询失败");
            map.put("rows", list);
            map.put("total", 0);
        }
        return map;
    }
}
